package ift604.projet.music;

import static ift604.projet.music.Song.songFromStereoMoodJSON;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.json.JSONException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.util.Log;

public class Playlist {
	private static String TAG = "PlayList";
    private List<Song> songs = new ArrayList<Song>();
    private static MediaPlayer mediaPlayer;
    private Song currentlyPlayingSong;

    public static Playlist playListFromStereoMoodJSON(JSONArray songsAsJson) throws JSONException {
        Playlist playlist = new Playlist();
        for(int i = 0; i < songsAsJson.size(); i++){
            JSONObject songAsJSON = (JSONObject) songsAsJson.get(i);
            playlist.songs.add(songFromStereoMoodJSON(songAsJSON));
        }
        return playlist;
    }
    
    public static Playlist playListFromDB(Cursor cursor) {
        Playlist playlist = new Playlist();

        cursor.moveToFirst();
    	while (!cursor.isAfterLast()) {
            playlist.songs.add(Song.songFromDB(cursor));
    		
            cursor.moveToNext();
    	}
    	
        return playlist;
    }

    public static Playlist emptyPlaylist() {
        return new Playlist();
    }

    private Playlist(){
    }

    public List<Song> songs(){
        return songs;
    }

    public void playRandomSong() {
        int numberOfSongs = songs().size();
        
        if (numberOfSongs <= 0)
        	return;
        
        int songIndex = new Random().nextInt(numberOfSongs);
        currentlyPlayingSong = playSongAtIndex(songIndex);
    }

    public Song playNextSong(){
        int currentSongIndex = songs.indexOf(currentlyPlayingSong);
        int nextIndex = currentSongIndex + 1 >=  songs.size() ? 0 : currentSongIndex + 1;
        currentlyPlayingSong = playSongAtIndex(nextIndex);
        return currentlyPlayingSong;
    }

    public Song playSongAtIndex(int songIndex) {
        Song song = songs().get(songIndex);
        playSongWithURL(song.fileURL());
        currentlyPlayingSong = song;
        return song;
    }
    
    public Song songFromURL(String URL) {
    	for (Song song: songs) {
    		if (song.fileURL().equalsIgnoreCase(URL)) {
    			return song;
    		}
    	}
    	return null;
    }

    public int idxFromURL(String URL) {
    	Song song = songFromURL(URL);
    	if (song == null)
    		return -1;
    	return songs.indexOf(song);
    }

	public class MediaTask extends AsyncTask<Void, Void, Void> {
		final String TAG = getClass().getName();
		private String URL;

		public MediaTask(String URL) {
			this.URL = URL;
		}

		@Override
		protected Void doInBackground(Void... params) {
            try {
                mediaPlayer.setDataSource(URL);
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (Exception e) {
                Log.e(TAG, e.toString());
            }

			return null;
		}
	}

    public synchronized void playSongWithURL(String URL) {
        	try {
        		if (mediaPlayer != null)
        			mediaPlayer.stop();
        	}
        	catch (Exception e) {
                Log.e(TAG, e.toString());
        	}
        	
        	mediaPlayer = new MediaPlayer();
        	
        	new MediaTask(URL).execute();        	
    }
    
    public Song currentlyPlayingSong(){
        return currentlyPlayingSong;
    }
    
    public synchronized void pause() {
    	try {
    		mediaPlayer.pause();
    	}
    	catch (Exception e) {}
    }
    
    public synchronized void resume() {
    	try {
    		mediaPlayer.start();
    	}
    	catch (Exception e) {}
    }

}